package com.wit.witmg.service.impl;

import com.google.common.base.Strings;
import com.wit.witmg.entity.TLog;
import com.wit.witmg.mapper.TLogMapper;
import com.wit.witmg.service.ITLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Enumeration;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bob
 * @since 2024-12-23
 */
@Service
public class TLogServiceImpl extends ServiceImpl<TLogMapper, TLog> implements ITLogService {

    @Override
    public void exceptLog(HttpServletRequest req, int type, String msg) {
        StringBuilder sb = new StringBuilder();
        Enumeration<String> handNames=req.getHeaderNames();
        while (handNames.hasMoreElements()) {
            String paraName = handNames.nextElement();
            String paraVal = req.getHeader(paraName);
            if (!Strings.isNullOrEmpty(paraName)) {
                sb.append(paraName).append("=").append(paraVal).append("&");
            }
        }
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String paraName = parameterNames.nextElement();
            String paraVal = req.getParameter(paraName);
            if (!Strings.isNullOrEmpty(paraName)) {
                sb.append(paraName).append("=").append(paraVal).append("&");
            }
        }
        TLog t=new TLog();
        t.setType(type);
        t.setMsg(sb +msg);
        t.setJoint(req.getRequestURI());
        t.setIp(req.getRemoteAddr());
        t.setDate(new Date());
        save(t);
    }

    @Override
    public void log(int type,String msg) {
        TLog t=new TLog();
        t.setType(type);
        t.setMsg(msg);
        t.setDate(new Date());
        save(t);
    }
}
